//
//  NewsImageListViewController.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/22.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "NewsImageListViewController.h"
#import "Header.h"
#import "ImageCollectionViewCell.h"
#import "loadData.h"
#import "ImageModal.h"
#import "PhotoViewController.h"
@interface NewsImageListViewController ()

{
    UICollectionView *_collectionView;
    NSMutableArray *_imageModalArray;
}

@end

@implementation NewsImageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self _creatCollectionView];
    
    [self loadData];
    
//    self.view.backgroundColor = [UIColor redColor];

}


- (void)loadData
{
    _imageModalArray = [[NSMutableArray alloc]init];
    
    
    NSArray *array = [loadData loadDataWithResourceName:@"image_list.json"];
    
    for (NSDictionary  *dic in array) {
        
        [dic objectForKey:@"image"];
        
        
        //创建modal，数据解析出来给modal，以便以后可以方便的使用
        ImageModal *modal = [[ImageModal alloc]init];
        
        [modal setValuesForKeysWithDictionary:dic];
        
        
        [_imageModalArray addObject:modal];
        
    }
    
}

- (void)_creatCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.itemSize = CGSizeMake((kwidth-50)/4, (kwidth-50)/4);
    

    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    
    //注册cell
    [_collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  _imageModalArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ImageCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    cell.modal = _imageModalArray[indexPath.row];
    
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    PhotoViewController *vc = [[PhotoViewController alloc]init];
    vc.index = indexPath.row;
    
    //抽取url
    NSMutableArray *imageUrlArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<_imageModalArray.count; i++) {
        
        ImageModal *modal = _imageModalArray[i];
        NSString *urlStr = modal.image;
        
        
        [imageUrlArray addObject:urlStr];
    }
    
//    vc.view.backgroundColor = [UIColor grayColor];
    
    
    //这里有imageUrlArray的set方法，在imageUrlArray的set方法里面传递数据
    vc.imageUrlArray = imageUrlArray;
    
    
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    
    

    
    
    //模态视图
    [self presentViewController:nav animated:YES completion:nil];
    
    
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
